package edu.uts.reekie.designer.execution.figures;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.draw2d.*;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.handles.HandleBounds;

import edu.uts.reekie.designer.figures.AnchorFigure;
import edu.uts.reekie.designer.figures.ConnectionAnchorProvider;
import edu.uts.reekie.designer.figures.NameableFigure;

public class PassiveComponentFigure extends Figure implements NameableFigure, ConnectionAnchorProvider{
	
	private static final int SQUARE_WIDTH = 25;
	private static final int SQUARE_HORIZONTAL_PAD = 4;
	private static final int SQUARE_VERTICAL_PAD =2;
	
	private static final Dimension SQUARE_SIZE = new Dimension(SQUARE_WIDTH+SQUARE_HORIZONTAL_PAD, SQUARE_WIDTH+SQUARE_VERTICAL_PAD);
	
	private Dimension textSize;
	
	private String name;
	
	
	public PassiveComponentFigure(){
		this("");
	}
	
	public PassiveComponentFigure(String name) {
		
		// set the font
		Font f = new Font(null,"Arial",9,SWT.NORMAL);
		setFont(f);
		
		setName(name);
		
		setLayoutManager(new StackLayout());
		
		// set the fg/bg colours
		setBackgroundColor(ColorConstants.black);
		setForegroundColor(ColorConstants.black);
		
	}
	
	public ConnectionAnchor getAnchor() {
		return new SquareAnchor(this);
	}
	
	class SquareAnchor extends ChopboxAnchor {
		
		
		public SquareAnchor(PassiveComponentFigure owner){
			super(owner);
		}
		
		private Rectangle getCircleBounds() {
			Rectangle ownerBounds = getOwner().getBounds();
			Rectangle circleBounds = ownerBounds.getIntersection(new Rectangle(ownerBounds.x,ownerBounds.y,SQUARE_WIDTH,SQUARE_WIDTH));
			return circleBounds;
		}

		protected Rectangle getBox() {
			return getCircleBounds();
		}
		
		public Point getReferencePoint() {
			
			Point ref = getCircleBounds().getCenter();
			getOwner().translateToAbsolute(ref);
			return ref;
			
		}
		
		
	}
	
	
	public Dimension getPreferredSize(int wHint, int hHint) {
		Dimension size = SQUARE_SIZE.getCopy();
		
		// expand the circle size horizontally by the text size
		if (textSize.height>size.height){
			size.expand(textSize.width, textSize.height - size.height);
		} else {
			size.expand(textSize.width,0);
		}
		return size;
	}
	
	Dimension getNameDimension() {
		TextUtilities tu = new TextUtilities();
		return tu.getTextExtents(name, getFont());
	}
	
	Rectangle getSquareRect() {
		Rectangle r = getBounds().getCopy();
		r.setSize(SQUARE_WIDTH, SQUARE_WIDTH);
		return r;
	}
	
	protected void paintFigure(Graphics g){
		
		Rectangle circleRect = getBounds().getCopy();
		Rectangle textRect = getBounds().getCopy();
		circleRect.setSize(SQUARE_WIDTH, SQUARE_WIDTH);

		//draw main area
		g.fillRectangle(circleRect);
		
		// draw the main text if it's available
		if (name==null || name.equalsIgnoreCase("")){
		} else {
			textRect.translate(SQUARE_SIZE.width, 5);
			g.drawText(name,textRect.x,textRect.y);
		}
	}

	public String getName() {
		
		return name;
	}

	public void setName(String newName) {
		this.name = newName;	
		textSize = getNameDimension();
	}
	

	protected boolean useLocalCoordinates(){return false;}
	

}
